﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Alimentador.Foods;

namespace Alimentador.CustomControls
{
    /// <summary>
    /// Interaction logic for WeekDay.xaml
    /// </summary>
    public partial class WeekDay : TabItem
    {
        public delegate void FoodAddedEventDelegate(FoodBase foodAdded);
        public event FoodAddedEventDelegate FoodAddedEvent;

        public delegate void FoodRemovedEventDelegate(FoodBase foodAdded);
        public event FoodRemovedEventDelegate FoodRemovedEvent;

        public delegate void MealQuantityChangeDelegate();
        public event MealQuantityChangeDelegate MealQuantityChangeEvent;

        private MealCtrl MealDescription = new MealCtrl();

        private List<MealCtrl> m_MealList = new List<MealCtrl>();

        /// <summary>
        /// Constructor
        /// </summary>
        public WeekDay()
        {
            InitializeComponent();

            MealDescription.MealDescription = "Pequeno-almoço";
            MealDescription.MealFoodAddedEvent += newMeal_FoodAddedEvent;
            MealDescription.MealFoodRemovedEvent += newMeal_FoodRemovedEvent;

            MealDescription.GotChildFocus += newMeal_GotChildFocus;
            MealDescription.MealQuantityChangeEvent += newMeal_MealQuantityChangeEvent;

            m_MealList.Add(MealDescription);
            UpdateList();

            addMealInstance.AddMealEvent += AddMealCtrl_AddMealEvent;
            addMealInstance.RemoveMealEvent += AddMealCtrl_RemoveMealEvent;

            MealsListView.SelectionChanged += MealsListView_SelectionChanged;
        }

        void MealsListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (MealCtrl mc in MealsListView.Items)
            {
                if (MealsListView.SelectedItem != ((System.Windows.Controls.Primitives.Selector)(((ListView)(e.Source)))).SelectedItem)
                {
                    mc.UnselectedChilds();
                }
            }
        }

        public MealCtrl AddMealCtrl_AddMealEvent(string mealName)
        {
            bool isDuplicated = false;
            foreach (MealCtrl mc in m_MealList)
            {
                if (mc.MealDescription == mealName)
                {
                    isDuplicated = true;
                    return mc;
                }
            }

            if (!isDuplicated)
            {
                MealCtrl newMeal = new MealCtrl();
                newMeal.MealDescription = mealName;
                newMeal.MealFoodAddedEvent += newMeal_FoodAddedEvent;
                newMeal.MealFoodRemovedEvent += newMeal_FoodRemovedEvent;
                newMeal.GotChildFocus += newMeal_GotChildFocus;
                newMeal.MealQuantityChangeEvent += newMeal_MealQuantityChangeEvent;
                m_MealList.Add(newMeal);
                UpdateList();
                //MealsListView.Items.Add(newMeal);

                MealsListView.SelectedItem = newMeal;

                return newMeal;
            }
            return null;
        }

        private void UpdateList()
        {
            MealsListView.ItemsSource = m_MealList;
            ICollectionView view = CollectionViewSource.GetDefaultView(MealsListView.ItemsSource);
            view.Refresh();
        }

        public List<MealCtrl> GetMealList
        {
            get
            {
                return m_MealList;  
            }
            set
            {
                m_MealList = value;
            }
        }

        void newMeal_GotChildFocus(MealCtrl mc)
        {
            MealsListView.SelectedItem = mc;
        }

        void newMeal_MealQuantityChangeEvent()
        {
            if (MealQuantityChangeEvent != null)
            {
                MealQuantityChangeEvent();
            }
        }

        void newMeal_FoodAddedEvent(FoodBase foodAdded)
        {
            if (FoodAddedEvent != null)
            {
                FoodAddedEvent(foodAdded);
            }
        }

        void newMeal_FoodRemovedEvent(FoodBase foodRemoved)
        {
            if (FoodRemovedEvent != null)
            {
                FoodRemovedEvent(foodRemoved);
            }
        }

        private void AddMealCtrl_RemoveMealEvent()
        {
            if (MealsListView.SelectedIndex >= 0)
            {
                MealCtrl mc = (MealCtrl)MealsListView.SelectedItem;
                bool hasRemoved = mc.RemoveSelectedIndex();
                if (hasRemoved)
                {
                    return;
                }

                MessageBoxResult msbr = MessageBox.Show("Pretende remover a refeição selecionada?", "Aviso", MessageBoxButton.YesNo, MessageBoxImage.Warning);
                if (msbr == MessageBoxResult.Yes)
                {
                    RemoveMeal();
                }
            }
        }

        public void RemoveMeal()
        {
            if (MealsListView.SelectedIndex >= 0)
            {
                MealCtrl mc = MealsListView.SelectedItem as MealCtrl;
                if (mc != null)
                {
                    mc.RemoveSelectedIndex(true);
                    m_MealList.Remove(mc);
                    UpdateList();
                }
            }
        }

    }
}